package com.qf.controller.business;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.Evaluate;
import com.qf.entitys.RespBean;
import com.qf.service.EvaluateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/*
* 商家的评价
* */
@Controller
@RequestMapping("/business/evaluate")
public class BusinessEvaluateController {
    @Resource
    private EvaluateService service;

    @GetMapping("/list")
    public String list(Evaluate evaluate,Integer pageindex,HttpServletRequest request){
        PageInfo<Evaluate> evaluatePageInfo = service.selectList(evaluate, pageindex);
        request.setAttribute("pageInfo",evaluatePageInfo);
        return "business/evaluatelist";
    }

    @GetMapping("/review")
    public String add(Integer id,HttpServletRequest request){
        Evaluate evaluate = service.selectByPrimaryKey(id);
        request.setAttribute("e",evaluate);
        return "business/review";
    }

    @PostMapping("/review")
    @ResponseBody
    public RespBean add(Evaluate evaluate) throws IOException {
        int i = service.updateByPrimaryKeySelective(evaluate);
        if (i>0){
            return RespBean.success("回评成功!");
        }else {
            return RespBean.error("回评失败!");
        }
    }
    @GetMapping("/del")
    @ResponseBody
    public RespBean delReview(Evaluate evaluate,Integer id){
        try {
            evaluate.setReview("");
            evaluate.setEvaluateId(id);
            int i = service.updateByPrimaryKeySelective(evaluate);
            if (i>0){
                return RespBean.success("回评删除成功");
            }else {
                return RespBean.error("回评删除失败");
            }
        }catch (Exception ex){
            return RespBean.error("回评删除失败");
        }
    }

}
